import logging

from pylons import request, response, session, tmpl_context as c
from pylons.controllers.util import abort, redirect_to
from authkit.authorize.pylons_adaptors import authorized
from authkit.permissions import HasAuthKitRole, ValidAuthKitUser, And

from arkaiv.lib.base import BaseController, render, model

log = logging.getLogger(__name__)

class CollectionController(BaseController):

    def index(self):
        # Not really used
        return render('/collections.mak')

    #@authorize(ValidAuthKitUser())
#    @authorize(HasAuthKitRole('admin'))
    def list(self):
        if authorized(HasAuthKitRole('admin')):
            c.collections = model.getcollections()
            return render('/collectionsadmin.mak')
        #else:
        if authorized(HasAuthKitRole('internal')):
            c.collections = model.getcollectionswithrole('internal')
            publiccollections = model.getcollectionswithrole('public')
            c.collections = c.collections + publiccollections
            return render('/collections.mak')
        else:
            c.collections = model.getcollectionswithrole('public')
            return render('/collections.mak')

    def displaycollection(self):
        collid = request.params['id']
        c.collname = request.params['name']
        c.items = model.getitemsincollection(collid)
        return render('/collectionexpanded.mak')

    def addcollection(self):
        collection = request.params['newcollection']
        role = request.params['role']
        try:
            access = request.params['pri']
        except:
            access = 'public'
        model.addnewcollection(collection, role, access)
        c.collections = model.getcollections()
        return render('/collections.mak')

    def deletecollection(self):
        model.deletecollection(request.params['id'])
        c.collections = model.getcollections()
        return render('/collections.mak')

